//
//  CJVisitView.swift
//  SwiftDemo
//
//  Created by Chris on 2017/6/25.
//  Copyright © 2017年 chris. All rights reserved.
//

import UIKit

class CJVisitView: UIView {
    
    
    /// 左边注册按钮
    var registerBtn = UIButton.cj_init("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    /// 右边登录按钮
    var loginBtn = UIButton.cj_init("登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    /// 根据字典信息设置未登录界面
    /// visitInfo: [imageName / message]
    var visitInfo: [String: String]? {
        didSet {
            guard let message = visitInfo?["message"], let imageName = visitInfo?["imageName"] else {
                return
            }
            // 首页时, 不设置
            if imageName == "" {
                startAnimation()
                return
            }
            
            midImgV.isHidden = true
            shadeImgV.isHidden = true
            
            tipL.text = message
            bgImgV.image = UIImage(named: imageName)
        }
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * Double.pi
        animation.duration = 15
        animation.isRemovedOnCompletion = false
        bgImgV.layer.add(animation, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 背景图
    fileprivate var bgImgV = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    /// 背景图
    fileprivate var shadeImgV = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    /// 中间图标
    fileprivate var midImgV = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    /// 提示label
    fileprivate var tipL = UILabel.cj_label("关注一些人, 回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
}

extension CJVisitView {
    
    fileprivate func setupUI() {
        
        backgroundColor = UIColor.cj_colorWithHex(hex: 0xEDEDED)
        
        // 添加视图
        addSubview(bgImgV)
        addSubview(shadeImgV)
        addSubview(midImgV)
        addSubview(tipL)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        tipL.textAlignment = .center
        
        // 取消AutoresizingMask
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置autolayout
        let margin: CGFloat = 20
        
        // 背景图
        addConstraint(NSLayoutConstraint(item: bgImgV, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: bgImgV, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        // 中间图标
        addConstraint(NSLayoutConstraint(item: midImgV, attribute: .centerX, relatedBy: .equal, toItem: bgImgV, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: midImgV, attribute: .centerY, relatedBy: .equal, toItem: bgImgV, attribute: .centerY, multiplier: 1.0, constant: 0))
        // 提示label
        addConstraint(NSLayoutConstraint(item: tipL, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipL, attribute: .top, relatedBy: .equal, toItem: bgImgV, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: tipL, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .left, relatedBy: .equal, toItem: tipL, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .top, relatedBy: .equal, toItem: tipL, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .right, relatedBy: .equal, toItem: tipL, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .top, relatedBy: .equal, toItem: registerBtn, attribute: .top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: registerBtn, attribute: .width, multiplier: 1.0, constant: 0))
        // 遮罩
        addConstraint(NSLayoutConstraint(item: shadeImgV, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: shadeImgV, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: shadeImgV, attribute: .centerY, relatedBy: .equal, toItem: midImgV, attribute: .centerY, multiplier: 1.0, constant: -35))
    }
}
